package com.school.revervationSys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.school.revervationSys.entity.Menu;
import com.school.revervationSys.entity.User;
import com.school.revervationSys.mapper.MenuMapper;
import com.school.revervationSys.service.MenuService;
import com.school.revervationSys.vo.MenuChild;
import com.school.revervationSys.vo.MenuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public List<MenuVo> menuList(String roleCode) {


        List<Menu> menus = menuMapper.menuList(roleCode);

        List<Menu> pamenu = menus.stream().filter(m -> m.getParentId().equals("0")).collect(Collectors.toList());
        List<MenuVo> list = new ArrayList<>();
        pamenu.forEach(p -> {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(p, menuVo);
            List<Menu> children = menuMapper.selectList(
                    new QueryWrapper<Menu>().lambda().eq(Menu::getParentId, p.getId())
            );
            menuVo.setChildren(children.stream().map(m -> {
                MenuChild menuChild = new MenuChild();
                BeanUtils.copyProperties(m, menuChild);
                return menuChild;
            }).collect(Collectors.toList()));
            list.add(menuVo);
        });

        return list;

    }


}
